package com.ourhealth.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.ourhealth.mapper.ReportMapper;
import com.ourhealth.pojo.Member;
import com.ourhealth.pojo.Order;
import com.ourhealth.pojo.Setmeal;
import com.ourhealth.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService{
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Map report(Date date) {
        String format = new SimpleDateFormat("yyyy").format(date);
        List<Member> members = reportMapper.getMember();
        List<Member> list = new ArrayList<>();
        for (Member member : members) {
            String year = new SimpleDateFormat("yyyy").format(member.getRegTime());
            if (year.equals(format)){
                list.add(member);
            }
        }
        Map map = new HashMap();
        int[] count = new int[12];
        for (Member member : list) {
            String month = new SimpleDateFormat("MM").format(member.getRegTime());
            for (int i = 0; i < 12; i++) {
                if (Integer.valueOf(month)<(i+1)){
                    count[i] ++;
                }
            }
        }

        int[] month = new int[12];
        for (int i = 0; i < month.length; i++) {
            month[i] = i+1;
        }
        map.put("months",month);
        map.put("memberCount",count);
        return map;
    }

    @Override
    public Map getSetmealReport() {

        List<Setmeal> list = reportMapper.getSetmealReport();

        List<String> setmealNames = new ArrayList<>();

        List<Map> setmealCountList = new ArrayList<>();

        Map<String,Object> map = new HashMap<>();


         for (Setmeal setmeal : list) {
        Map<String,Object> setmealCount = new HashMap<>();
            setmealNames.add(setmeal.getName());
            setmealCount.put("name",setmeal.getName());
            setmealCount.put("value",setmeal.getCount());
            setmealCountList.add(setmealCount);
        }
        map.put("setmealNames",setmealNames);
        map.put("setmealCount",setmealCountList);
        return map;
    }

    @Override
    public Map getBusinessReportData() {
        List<Member> members = reportMapper.getMember();
        List<Setmeal> setmeals = reportMapper.getSetmeal();
        List<Order> orders = reportMapper.getOrder();
        Date now = new Date();
        DateTime today = DateUtil.beginOfDay(now);
        DateTime weekBegin = DateUtil.beginOfWeek(now);
        DateTime month = DateUtil.beginOfMonth(now);

        Map<String,Object> map = new HashMap<>();
        Integer todayNewMember = 0;
        Integer weekNewMember = 0;
        Integer monthNewMember = 0;
        for (Member member : members) {
            //注册事件在今天之后
            if (member.getRegTime().after(today)){
                todayNewMember++;
            }
            map.put("todayNewMember",todayNewMember);

            if (member.getRegTime().after(weekBegin)){
                weekNewMember++;
            }
            map.put("thisWeekNewMember",weekNewMember);

            if (member.getRegTime().after(month)){
                monthNewMember++;
            }
            map.put("thisMonthNewMember",monthNewMember);
            map.put("totalMember",members.size());

        }

        Integer todayOrderNumber = 0;
        Integer todayVisitsNumber = 0;
        Integer thisWeekOrderNumber = 0;
        Integer thisWeekVisitsNumber = 0;
        Integer thisMonthOrderNumber = 0;
        Integer thisMonthVisitsNumber = 0;
        for (Order order : orders) {

            //注册时间在今天之后
            if (order.getOrderDate().after(today)){
                todayOrderNumber++;
                if (order.getOrderStatus().equals("到诊")){
                    todayVisitsNumber++;
                }
            }
            map.put("todayOrderNumber",todayOrderNumber);
            map.put("todayVisitsNumber",todayVisitsNumber);

            if (order.getOrderDate().after(weekBegin)){
                thisWeekOrderNumber++;
                if (order.getOrderStatus().equals("到诊")){
                    thisWeekVisitsNumber++;
                }
            }
            map.put("thisWeekOrderNumber",thisWeekOrderNumber);
            map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);

            if (order.getOrderDate().after(month)){
                thisMonthOrderNumber++;
                if (order.getOrderStatus().equals("到诊")){
                    thisMonthVisitsNumber++;
                }
            }
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        }

        Integer setmealTotal = reportMapper.getSetmealTotal();
        List<Map> list = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            Map<String,Object> meals = new HashMap<>();
            meals.put("name",setmeal.getName());
            meals.put("setmeal_count",setmeal.getCount());
            meals.put("proportion",(double)setmeal.getCount()/(double)setmealTotal);
            list.add(meals);
        }
        map.put("hotSetmeal",list);
        return map;
    }

    @Override
    public Map getMemberReportByDate(String dateBeginString, String dateEndString) {
        DateTime dateBegin = DateUtil.parse(dateBeginString);
        DateTime dateEnd = DateUtil.parse(dateEndString);
        int l = (int)DateUtil.betweenMonth(dateBegin, dateEnd, true);
        List<Member> members = reportMapper.getMemberByDate(DateUtil.beginOfDay(dateBegin),DateUtil.beginOfDay(dateEnd));
        Map<String,Object> map = new HashMap<>();
        List<String> months = new ArrayList<>();

        for (int i = 0; i < l; i++) {
            DateTime offset = DateUtil.offsetMonth(dateBegin,i);
            months.add(offset.toString("yyyy-MM"));
        }

        int[] memberCount = new int[l];
        for (Member member : members) {
            for (int i = 0; i < l; i++) {
                DateTime offset = DateUtil.offsetMonth(dateBegin,i);
                if (DateUtil.beginOfMonth(member.getRegTime()).equals(offset)){
                    memberCount[i]++;
                    break;
                }
            }
        }
        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }

    /**查询用户男女比例和年龄段比例
     * @return
     */
    @Override
    public Map<String, Object> getSexReport() {
        Map<String,Object> map = new HashMap<>();
        //查询男女性别比例占比
        List<Integer> sexReport = reportMapper.getSexReport();
        map.put("sexReport",sexReport);

        //查询年龄段比例占比
        List<Map<String,Object>> sexcountReport=reportMapper.getSexCountReport();
        map.put("ageCount",sexcountReport);
        List<String> memberAge = new ArrayList<>();
        for (Map<String, Object> maplist : sexcountReport) {
            String name = (String) maplist.get("name");
            memberAge.add(name);
        }

        return  map;



    }
}
